package team.seekers.oj.codetest.utils;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.assertj.core.util.Lists;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class ListToByteUtil {
    public static List<Long> codeTestUsersToUserIdList(byte[] users) {
        if (users == null) {
            return Lists.newArrayList();
        }
        ByteBuffer wrap = ByteBuffer.wrap(users);
        List<Long> codeTestUserIdList = new ArrayList<>(users.length / 8);
        for (int i = 0, n = users.length; i < n; i += 8) {
            codeTestUserIdList.add(wrap.getLong(i));
        }
        return codeTestUserIdList;
    }

    public static byte[] codeTestUsersFromUserIdList(List<Long> codeTestUserIdList) {
        if(codeTestUserIdList != null){
            ByteBuf byteBuf = Unpooled.buffer(codeTestUserIdList.size() * 8);
            codeTestUserIdList.forEach(byteBuf::writeLong);
            return byteBuf.array();
        }else{
            return null;
        }

    }

    public static List<Long> codeTestProblemsToProblemIdList(byte[] problems) {
        if (problems == null) {
            return Lists.newArrayList();
        }
        ByteBuffer wrap = ByteBuffer.wrap(problems);
        List<Long> codeTestProblemIdList = new ArrayList<>(problems.length / 8);
        for (int i = 0, n = problems.length; i < n; i += 8) {
            codeTestProblemIdList.add(wrap.getLong(i));
        }
        return codeTestProblemIdList;
    }

    public static byte[] codeTestProblemsFromProblemIdList(List<Long> codeTestProblemIdList) {
        if(codeTestProblemIdList != null){
            ByteBuf byteBuf = Unpooled.buffer(codeTestProblemIdList.size() * 8);
            codeTestProblemIdList.forEach(byteBuf::writeLong);
            return byteBuf.array();
        }else{
            return null;
        }

    }
}
